package com.hg.wap.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdiy.json.JsonObject;

import com.hg.util.action.WapMbaseAction;
import com.hg.util.common.DataValidateUtil;
import com.hg.util.common.SMSUtil;
import com.hg.util.common.Sec;
import com.hg.wap.service.UserService;
import com.hg.wap.vo.VO4UserLogin;
import com.hg.wap.vo.VO4user;

public class UserAction extends WapMbaseAction
{
	UserService service = new UserService();
	
	/**
	 * 登录界面
	 * 登录页跳转进入条件
	 * 1.未登录状态 login_status 0
	 * 2.需要验证安全登录状态的临时登录 login_status:2
	 * 3.下单时账户无手机号（进入绑定）
	 * 4.下单时有账户余额并且为临时登录状态
	 * 
	 * 登录页的展示：
	 * 1.无登录时，显示账号登录与手机短信登录（无需验证唯一）
	 * 2.临时登录时，显示验证登录，账号名锁死，如有手机号，手机号锁死
	 * 3.会员为临时会员，并且无手机号时，显示绑定页
	 */
	public void login(Map root){
		global_run(root);
		
		root.put("page_title", "登录");
		ftl_return(root);
	}
	
	/**
	 * 处理登录
	 * @param root
	 */
	public void dologin(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpServletResponse response = (HttpServletResponse) root.get("response");
		String user_name = request.getParameter("username");	//手机号码
		String password = request.getParameter("password");		//密码
		
		VO4UserLogin vo = new VO4UserLogin();
		vo.setUser_name(user_name);
		vo.setUser_pwd(password);
		
		
		String psw = Sec.MD5Encrypt(password, 32);
		
		/*if (dto == null) {
		return -1;
	} else {
		String psw = Sec.MD5Encrypt(vo.password, 32);
		if (dto.getUser_pwd().equals(psw)) {
			vo.setUserName(dto.getUser_name());
			return dto.getId();
		} else {
			return -2;
		}
	}*/
		
		VO4UserLogin voL = service.login(vo);
		if(null==voL){
			map.put("err_code", 1101);
			map.put("err_msg", "用户名不存在");
		}else if(psw.equals(voL.getUser_pwd())){
			initWapLogined(voL,root);
			
			map.put("err_code", -1);
			map.put("err_msg", "登录成功");
		}else{
			map.put("err_code", 1102);
			map.put("err_msg", "密码错误");
		}
		json_return(root, map);
	}
	
	/**
	 * 退出登录
	 * @param root
	 */
	public void loginout(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpServletResponse response = (HttpServletResponse) root.get("response");
		
		initWapLogout(request, response);
		
		try
		{
			response.sendRedirect(root.get("project_url")+"/"+root.get("folder_name")+"/"+root.get("module")+"/login.do");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 处理手机登录
	 * @param root
	 */
	public void dophlogin(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String mobile = request.getParameter("mobile");				//手机号码
		String sms_verify = request.getParameter("sms_verify");		//短信验证码
		
		map.put("err_code", 1111);
		map.put("err_msg", "待完成");
		json_return(root, map);
	}
	
	public void sendphCode(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpSession session = request.getSession();
		String phone = request.getParameter("phone");
		if (!DataValidateUtil.isMobile(phone)) {
			map.put("err_code", 1102);
			map.put("err_msg", "手机号码格式不对!");
		}else{
			if(service.ifRegistered(phone)){
				map.put("err_code", 1101);
				map.put("err_msg", "号码已经注册，请直接登录");
			}else{
				SMSUtil sms = new SMSUtil();
				JsonObject jo = sms.verify(request.getParameter("phone"));
				if("false".equals(jo.get("result"))){
					//System.out.println((String)jo.get("msg"));
					
					map.put("err_code", 1102);
					map.put("err_msg", "发送失败，原因为："+(String)jo.get("msg"));
					
				}else{
					//System.out.println("session.yanzhengma is "+(String)jo.get("code"));
					
					session.setAttribute("checkCode", (String)jo.get("code"));
					
					map.put("err_code", -1);
					map.put("err_msg", "已经发送，请在手机短信内查看验证码");
				}
			}
		}
		
		
		json_return(root, map);
	}
	
	/**
	 * 绑定手机号码
	 * @param root
	 */
	public void dophbind(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String mobile = request.getParameter("mobile");				//手机号码
		String sms_verify = request.getParameter("sms_verify");		//短信验证码
		
		map.put("err_code", 1111);
		map.put("err_msg", "待完成");
		json_return(root, map);
	}
	
	/**
	 * 注册界面
	 * @param root
	 */
	public void register(Map root){
		root.put("page_title", "注册");
		
		ftl_return(root);
	}
	
	/**
	 * 处理注册界面
	 * @param root
	 */
	public void doregister(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpServletResponse response = (HttpServletResponse) root.get("response");
		HttpSession session = request.getSession();
		
		String userName = ((String) session.getAttribute("user"));
		String checkCode = ((String) session.getAttribute("checkCode"));
		
		if(userName!=null ){
			map.put("err_code", 1101);
			map.put("err_msg", "已经存在登录账号，请先退出登录再注册");
		}else{
			if(checkCode!=null){
				//如果验证码正确
				if(checkCode.equals(request.getParameter("code"))){
					VO4UserLogin vo = new VO4UserLogin();
					vo.setUser_name(request.getParameter("phone"));
					vo.setMobile(request.getParameter("phone"));
					vo.setUser_pwd(request.getParameter("password"));
					
					vo.setLogin_ip(request.getRemoteAddr());
					
					vo =service.createUser(vo);
					
					initWapLogined(vo, root);
					
					map.put("err_code", -1);
					map.put("err_msg", "注册成功");
				}else{
					
					map.put("err_code", 1103);
					map.put("err_msg", "验证码错误");
				}
			}else{
				
				map.put("err_code", 1102);
				map.put("err_msg", "请先获取验证码");
			}
		}
		
		json_return(root, map);
	}
}
